package com.teamtwoseven.todo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * This is the first screen the user sees when the application starts up. It
 * allows the user to log in or create a new account.
 * 
 * @author teamtwoseven
 */
public class sysStart extends Activity implements OnClickListener {

	private Storage storage;
	private Button bLogin, bExit, bNewAccount;
	private EditText etUserID, etPassword;
	
	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 * 			allows instance data to be stored
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		
		storage = new Storage(sysStart.this);
		
		bLogin = (Button) findViewById(R.id.loginLayout_bLogin);
		bExit = (Button) findViewById(R.id.loginLayout_bExit);
		bNewAccount = (Button) findViewById(R.id.loginLayout_bNewAccount);
		etUserID = (EditText) findViewById(R.id.loginLayout_etUserID);
		etPassword = (EditText) findViewById(R.id.loginLayout_etPassword);

		bLogin.setOnClickListener(this);
		bExit.setOnClickListener(this);
		bNewAccount.setOnClickListener(this);
		
		if (storage.getRowID("default") < 0) {
			storage.createUser("default", "default", "default",
				"default@default.com");
		}
	}

	/**
	 * Handles button clicks
	 * 
	 * @param arg0
	 * 			the element that was clicked
	 */
	public void onClick(View arg0) {
		if (arg0 == bLogin) {
			String userID = etUserID.getText().toString();
			String password = etPassword.getText().toString();
			long rowID = verify(userID, password);
			
			if (rowID > -1) {
				// Pass the row ID to the Main screen
				
				Singleton singleton = Singleton.getInstance();
				singleton.setUserRowID(rowID);
				startActivity(new Intent("com.teamtwoseven.todo.MAIN"));
				finish();
			}
		}
		else if (arg0 == bNewAccount) {
			startActivity(new Intent("com.teamtwoseven.todo.NEWACCOUNT"));
		}
		else if (arg0 == bExit) {
			// End this Activity and exit application
			finish();
		}
	}

	/**
	 * Verifies the given userID exists and the password is correct. If not,
	 * displays why.
	 * 
	 * @param userID
	 * 			a user ID
	 * @param password
	 * 			a password
	 * @return the row ID for the user with that user ID, or -1 if no such user
	 */
	public long verify(String userID, String password) {
		boolean valid = false;
		long rowID = storage.getRowID(userID);
		
		if (rowID > -1) {
			valid = password.equals(storage.getPassword(rowID));
		}
		if (!valid) {
			rowID = -1; // else existing users can log in with any password
			
			// Display why
			popupDialog("Incorrect user ID or Password");
		}
		return rowID;
	}

	/**
	 * Displays the given message and an OK button in a dialog.
	 * 
	 * @param message
	 * 			text to display to the user
	 */
	private void popupDialog(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setMessage(message);
		builder.setCancelable(false);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		builder.show();
	}

	/**
	 * Clears the user ID and password text fields when a new Activity starts.
	 */
	@Override
	protected void onStop() {
		super.onStop();
		etUserID.setText("");
		etPassword.setText("");
	}
}